package thread.test_2;

import java.net.Socket;

public class Test11 {
    public static void main(String[] args) {
        Ticket ticket = new Ticket();
        Socket socket = new Socket();
        System.out.println(socket);
        /*new Thread(ticket,"窗口1").start();
        new Thread(ticket,"窗口2").start();
        new Thread(ticket,"窗口3").start();*/
    }
}
class Ticket implements Runnable {
    private int ticket = 100;
    private static final Object o = new Object();
    @Override
    public void run() {
        while (true) {
            // 方法一，使用synchronized给代码块上锁
            synchronized (o) {
                if (ticket <= 0){
                    break;
                } else {
                    try {
                        Thread.sleep(10);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                    System.out.println(Thread.currentThread().getName() + "正在卖第" + ticket-- + "张票");
                }
            }
        }
    }
}
